package com.mycompany;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;

public class WordListExport {
	
	File file = new File("wordList.pdf");
	
	public File getFile() {
		return file;
	}

	WordListExport(List<String[]> wordList) throws ColumnBuilderException, JRException, ClassNotFoundException {
			
			// build report
			JasperPrint jp = buildReport(wordList);

			// export report
			JRPdfExporter exporter = new JRPdfExporter();
			
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		 
			exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jp);
			exporter.setParameter(JRPdfExporterParameter.OUTPUT_FILE_NAME, file.getName());
			exporter.setParameter(JRPdfExporterParameter.IGNORE_PAGE_MARGINS,
					Boolean.TRUE);
			exporter.setParameter(JRPdfExporterParameter.OFFSET_X, 0);
			exporter.exportReport();

	}

	private static JasperPrint buildReport(List<String[]> wordList) throws ColumnBuilderException,	JRException, ClassNotFoundException {
		
		Style headerStyle = getHeaderStyle();
		Style detailStyle = new Style();
		detailStyle.setBorder(Border.THIN);

		// build first report
		FastReportBuilder report = getReport(headerStyle, detailStyle);
		DynamicReport dynaRep = report.build();

		// Prepare data
		Object[] obj = new Object[wordList.size()];

		for(int h = 0; h < wordList.size(); h++) {
			String[] wordListRow = wordList.get(h);
			obj[h] = new WordListBean(wordListRow[0], wordListRow[1], wordListRow[2], wordListRow[3]);
		}

		// generate jasper print dynamically
		JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynaRep,
				new ClassicLayoutManager(), new JRBeanArrayDataSource(obj));

	 
		return jp;
	}

	private static Style getHeaderStyle() {
		Style headerStyle = new Style();
		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
		headerStyle.getFont().setItalic(true);
		headerStyle.setBorderBottom(Border.PEN_1_POINT);
		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);

		headerStyle.setTextColor(Color.BLUE);
		headerStyle.setTransparency(Transparency.OPAQUE);
		return headerStyle;
	}

	private static AbstractColumn getColumn(String property, Class type,
			String title, int width, Style headerStyle, Style detailStyle)
			throws ColumnBuilderException {
		 
		Style cellStyle = new Style();
		cellStyle.setHorizontalAlign(HorizontalAlign.CENTER);
		cellStyle.setVerticalAlign(VerticalAlign.MIDDLE);
		
		AbstractColumn columnState = ColumnBuilder.getInstance()
				.setColumnProperty(property, type.getName()).setTitle(title)
				.setWidth(Integer.valueOf(width)).setStyle(detailStyle)
				.setStyle(cellStyle)
				.setHeaderStyle(headerStyle).build();
		 
		return columnState;
	}

	private static FastReportBuilder getReport(Style headerStyle,
			Style detailStyle) throws ColumnBuilderException,
			ClassNotFoundException {
		FastReportBuilder report = new FastReportBuilder();

		AbstractColumn columnFrom = getColumn("from", String.class, "From",
				30, headerStyle, detailStyle);
		AbstractColumn columnTo = getColumn("to", String.class,
				"To", 30, headerStyle, detailStyle);
		AbstractColumn columnWord = getColumn("word",
				String.class, "Word", 50, headerStyle, detailStyle);
		AbstractColumn columnTranslation = getColumn("translation", String.class, "Translation(s)", 50,
				headerStyle, detailStyle);
		 
		
		report.addColumn(columnFrom).addColumn(columnTo)
					.addColumn(columnWord).addColumn(columnTranslation);
		
		Style reportTitleStyle = new Style();
		 
		
		report.setTitle("Word List");

		
		report.setUseFullPageWidth(true); // make columns to fill the page
												// width

		return report;
	}

}